﻿<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { BasicTableColumn } from '@/types'
import {
  Department,
  DepartmentAddType,
  DepartmentEditType,
  ConditionSelectorParams
} from '@/types/modules/department'
import { formatToDateTime } from '@/utils/dateUtil'
import { BasicModal as BModal } from '@/components/Modal'
import { useModal } from '@/hooks/system/useModal'
import { useDrawer } from '@/hooks/system/useDrawer'
import LeaderDetail from '@/views/user/detail.vue'
import ParentDetail from '@/views/department/detail.vue'
import RolesDetail from '@/views/role/detail.vue'
import { useI18n } from '@/hooks/web/useI18n'
import { useHttp } from '@/hooks/web/useHttp'
import { useTableList } from '@/hooks/system/useTableList'
import * as api from '@/api/modules/department'
import search from './components/search.vue'
import list from './components/list.vue'
import add from './add.vue'
import edit from './edit.vue'
import detail from './detail.vue'

const { t } = useI18n()

const searchParams: ConditionSelectorParams = reactive({})

let basicData: BasicTableColumn<Department>[] = [
  {
    title: '部门名称',
    dataIndex: 'name'
  },
  {
    title: '负责人',
    dataIndex: 'leader',
    isCheck: true,
    name: 'name',
    ellipsis: true
  },
  {
    title: '上级部门',
    dataIndex: 'parent',
    isCheck: true,
    name: 'name',
    ellipsis: true
  },
  {
    title: '角色',
    dataIndex: 'roles',
    isCheck: true,
    name: 'name',
    align: 'center'
  },
]

const { columns, pageParams, tableData, total, getData } =
  useTableList<Department>(basicData)

onMounted(() => {
  getDataList()
})

const getDataList = getData<ConditionSelectorParams>(api.getConditionSelector, searchParams)

const onSearch = () => {
  pageParams.value.pageNum = 1
  getDataList()
}

const deleteDataHttp = useHttp(
  t('system.api.deleteError'),
  t('system.api.deleteSuccess')
)
const hanldeDeleteData = async (department: Department) => {
  const res = await api.deleteDepartment(department.id)
  deleteDataHttp(res, getDataList)
}

const addFormRef = ref()
const {
  bindValue: addBindValue,
  register: addRegister,
  onShow: onShowAdd,
  onClose: onCloseAdd,
  getInnerForm: getAddForm
} = useModal<Department>()
const handleAddResult = useHttp(
  t('system.api.addError'),
  t('system.api.addSuccess')
)

const onFinishAdd = async function () {
  const result = await getAddForm<DepartmentAddType>(addFormRef)
  if (result) {
    const res = await api.createDepartment(result)
    handleAddResult(res, () => {
      getDataList()
      onCloseAdd()
    })
  }
}

const editFormRef = ref()
const {
  bindValue: editBindValue,
  register: editRegister,
  currentItem: editCurrentItem,
  onShow: onShowEdit,
  onClose: onCloseEdit,
  getInnerForm: getEditForm
} = useModal<Department>()
const handleEditResult = useHttp(
  t('system.api.editError'),
  t('system.api.editSuccess')
)

const onFinishEdit = async function () {
  const result = await getEditForm<DepartmentEditType>(editFormRef)
  if (result) {
    const res = await api.updateDepartment(result, editCurrentItem.value?.id)
    handleEditResult(res, () => {
      getDataList()
      onCloseEdit()
  })
  }
}

const {
  bindValue: detailBindValue,
  register: detailRegister,
  currentItem: detailCurrentItem,
  onShow: onShowDetail,
  onClose: onCloseDetail
} = useModal<Department>()

const {
  visible: relateVisible,
  currentItem: relateCurrentItem,
  onShow: onShowRelate
} = useDrawer()

const relateComps = {
  leader: LeaderDetail,
  parent: ParentDetail,
  roles: RolesDetail,
}

const relateAttrName = ref<string>('')
const relateTitle = ref<string>('')
const onCheckRelate = (attrName: string, title: string, relate: any) => {
  relateAttrName.value = attrName
  relateTitle.value = title
  onShowRelate(relate)
}
</script>

<template>
  <div class="page-wrapper">
    <a-card class="search-card">
      <search :formState="searchParams" @search="onSearch" />
    </a-card>
    <a-card class="table-card">
      <list
        v-bind="{ pageParams, total, columns, tableData }"
        title="部门"
        @add="onShowAdd"
        @refresh="getDataList"
        @check="onCheckRelate"
      >
        <template #operate="row">
           <a type="link" @click="onShowDetail(row)">{{ t('system.detail') }}</a>
          <a-divider type="vertical" />
          <a type="link" @click="onShowEdit(row)">{{ t('system.edit') }}</a>
          <a-divider type="vertical" />
          <a-popconfirm
            :title="t('system.deletePrompt')"
            placement="left"
            @confirm="hanldeDeleteData(row)"
          >
            <a type="link" class="ap-action-danger-btn">{{
              t('system.delete')
            }}</a>
          </a-popconfirm>
        </template>
      </list>
    </a-card>  

    <b-modal
      v-bind="addBindValue"
      :title="t('system.modal.add')"
      @register="addRegister"
      @ok="onFinishAdd"
    >
      <add ref="addFormRef" />
    </b-modal>

    <b-modal
      v-bind="editBindValue"
      :title="t('system.modal.edit')"
      @register="editRegister"
      @ok="onFinishEdit"
    >
      <edit ref="editFormRef" :item-data="editCurrentItem" />
    </b-modal>

    <b-modal
      v-bind="detailBindValue"
      :footer="null"
      :title="t('system.modal.detail')"
      @register="detailRegister"
      @cancel="onCloseDetail"
    >
      <detail :item-data="detailCurrentItem" />
    </b-modal>

    <a-drawer
      v-model:visible="relateVisible"
      :title="relateTitle"
      width="40vw"
      placement="right"
    >
      <component
      :is="relateComps[relateAttrName]"
      :item-data="relateCurrentItem"
      />
    </a-drawer>
  </div>
</template>

<style lang="less" scoped>
.page-wrapper {
  padding: 20px;

  .table-card {
    margin-top: 20px;
  }
}

.drawer-footer {
  text-align: right;

  button {
    margin-left: 8px;
  }
}
</style>
